package com.android.happymall.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.ImageDetailFragment;
import com.android.happymall.utils.MyLog;
import com.android.happymall.view.group.HackyViewPager;

/**
 * Created by Administrator on 2017/9/1.
 */

public class ImagePagerActivity extends FragmentActivity {
       private static final String STATE_POSITION = "STATE_POSITION";
      public static final String EXTRA_IMAGE_INDEX = "image_index";
        public static final String EXTRA_IMAGE_URLS = "image_urls";
    public static final String EXTRA_IMAGE_CURRENTID = "image_currentid";
        private HackyViewPager mPager;
       private int pagerPosition;
        private TextView indicator;
       private int currentid;
    private String[] urls;
            @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
           setContentView(R.layout.image_detail_pager);
             pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
                Bundle b=this.getIntent().getExtras();
                urls=b.getStringArray(EXTRA_IMAGE_URLS);
                MyLog.e("urls",urls.length+"");
                currentid = getIntent().getIntExtra(
                        EXTRA_IMAGE_CURRENTID,0);
         mPager = (HackyViewPager) findViewById(R.id.pager);
            ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager() );
                mPager.setAdapter(mAdapter);
                indicator = (TextView) findViewById(R.id.indicator);
             CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
               indicator.setText(text);
                // 更新下标
                mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                    }
                        @Override
                        public void onPageScrolled(int arg0, float arg1, int arg2) {
                        }
                        @Override
                        public void onPageSelected(int arg0) {
                               CharSequence text = getString(R.string.viewpager_indicator,
                                              arg0 + 1, mPager.getAdapter().getCount());
                              indicator.setText(text);
                         }

                          });
                if (savedInstanceState != null) {
                      pagerPosition = savedInstanceState.getInt(STATE_POSITION);
                   }
               mPager.setCurrentItem(pagerPosition);
          }
               @Override
      public void onSaveInstanceState(Bundle outState) {
               outState.putInt(STATE_POSITION, mPager.getCurrentItem());
           }
               private class ImagePagerAdapter extends FragmentStatePagerAdapter {

                   public ImagePagerAdapter(FragmentManager fm) {
                     super(fm);
                  }
                   @Override
                public int getCount() {
                       return urls.length;
                  }
                   @Override
                public Fragment getItem(int position) {

                    String url = urls[position];
                       return ImageDetailFragment.newInstance(url);
                    }
        }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);
    }
}

